package com.seven.z7.logviewer.xml;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * 
 * @author aprokofjev
 * 
 */
public abstract class XMLParser {
    protected static final String SEPARATOR = "-";
    protected static final String NODE_MENU = "menu";
    protected static final String NODE_SUB_MENU = "submenu";
    protected static final String NODE_ITEM = "item";
    protected static final String NODE_ATTR_TEXT = "text";
    protected static final String NODE_ATTR_ICON = "icon";
    protected static final String NODE_ATTR_KEY_CODE = "key";
    protected static final String NODE_ATTR_MNEMONIC = "mnemonic";
    protected static final String NODE_ATTR_MODIFIER = "modifier";
    protected static final String NODE_ATTR_ENABLE = "enable";

    private static final int HEX_RADIX = 16;

    private Node mRootNode;

    public XMLParser(File file) throws SAXException, ParserConfigurationException, IOException {
        if (!isXMLValid(file)) {
            throw new SAXException("ERROR! \"" + file.getName() + "\" is not valid!");
        }

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse(file);
        doc.getDocumentElement().normalize();
        String root = doc.getDocumentElement().getNodeName();

        NodeList nodeLst = doc.getElementsByTagName(root);
        mRootNode = nodeLst.item(0);
    }

    public abstract void parse();

    protected Node getRootNode() {
        return mRootNode;
    }

    protected String getAttribute(Node n, String name) {
        NamedNodeMap attrs = n.getAttributes();

        if (attrs != null) {
            Attr atr = (Attr) attrs.getNamedItem(name);
            if (atr != null) {
                return atr.getValue();
            }
        }
        return "";
    }

    protected Integer getIntAttribute(Node n, String name) {
        try {
            return Integer.parseInt(getAttribute(n, name));
        } catch (NumberFormatException e) {
            return null;
        }
    }

    protected Integer getHexAttribute(Node n, String name) {
        try {
            return Integer.parseInt(getAttribute(n, name), HEX_RADIX);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    protected int getIntValue(Node n, String name) {
        Integer val = getIntAttribute(n, name);
        return val != null ? val.intValue() : 0;
    }

    protected boolean isXMLValid(File file) throws SAXException {
        return file != null;
    }

    protected boolean getBooleanAttribute(Node n, String name) {
        return Boolean.parseBoolean(getAttribute(n, name));
    }

}
